package com.dandan.tzx.router;

import android.content.Context;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.service.SerializationService;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;

import java.lang.reflect.Type;

/**
 * Created by tanzhenxing
 * Date: 2018/1/12 下午3:15
 * Description:
 */
@Route(path = "/ddservice/json")
public class JsonServiceImpl implements SerializationService {
    private Gson gson;
    @Override
    public void init(Context context) {
        Log.d("tanzhenxing", "JsonServiceImpl init");
        gson = new Gson();
    }

    @Override
    public <T> T json2Object(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
        //return gson.fromJson(text, clazz);
    }

    @Override
    public String object2Json(Object instance) {
        return JSON.toJSONString(instance);
        //return gson.toJson(instance);
    }

    @Override
    public <T> T parseObject(String input, Type clazz) {
        return JSON.parseObject(input, clazz);
        //return gson.fromJson(input, clazz);
    }
}